<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Worker extends CH_Controller{

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	 /**
         * Constructor
         */
	function __construct() { 
		parent::__construct(); 
		$this->init();
		//$this->load->helper(array('form','array', 'url','common','login','ckeditor'));
		$this->load->model(array('common_model','worker_model','setting_model'));
    } 
    	
	/**
	 * 설교방송 목록
	 *
	 * @access	public
	 * @return	result
	 */
	function workerList(){
		login_admin();
		$url = $this->uri->segment(1).'/'.$this->uri->segment(2);
		$uri_array = segment_explode($this->uri->uri_string());
		$array_segment = array_keys($this->uri->segment_array(), "page");
		$page = in_array('page', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'page'))) : 1;
		$uri_segment = in_array('page', $uri_array) ? $array_segment[0]+1 : 0;
		$param['sch_duty'] = urldecode($this->security->xss_clean(url_explode($uri_array, 'sch_duty')));
		$param['keyname'] = urldecode($this->security->xss_clean(url_explode($uri_array, 'keyname')));
		$param['keyword'] = urldecode($this->security->xss_clean(url_explode($uri_array, 'keyword')));
		$param['page'] = $page;
		$param['listnum'] = 8;
		
		$count = $this->worker_model->list_total($param);
		$result = $this->worker_model->worker_list($param);
		
		$this->load->library('pagination');
		$config['base_url'] = '/worker/workerList';
		$config['total_rows'] = $count;
		$config['per_page'] = $param['listnum'];
		$config['use_page_numbers'] = true;
		
		$config['uri_segment'] = $uri_segment;
		$this->pagination->initialize($config);
		
		
		$list_no = $count - $param['listnum'] * ($page-1);
		$data = array(
				'_title' => '설교방송관리',
				'_group' => 'homepage',
				'_content' => $this->config->item('ch_manager_root').'/'.$url,
				'rst' => $result,
				'paging'=> $this->pagination->create_links(),
				'list_no'=> $list_no,
				'page'=>$page,
				'code_duty'=> $this->common_model->select_child_code(2),
				'param'=>$param
		);
		return $data ;
	}
	
	/**
	 * 설교방송 폼
	 *
	 * @access	public
	 * @return	result
	 */
	function workerForm(){
		login_admin();
		$uri_array = segment_explode($this->uri->uri_string());
		$page = in_array('page', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'page'))) : 1;
		$worker_id = in_array('id', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'id'))) : null;
		$url = $this->uri->segment(1).'/'.$this->uri->segment(2);
				
		$data = array(
				'_title' => '교역자관리',
				'_group' => 'homepage',
				'_content' => $this->config->item('ch_manager_root').'/'.$url,
				
		);

		$data = array_merge($data,array('code_duty'=> $this->common_model->select_child_code(2),'code_title'=> $this->common_model->select_child_code(3)),$this->worker_model->select($worker_id));
		return $data ;
	}
	
	/**
	 * 설교방송 db 처리
	 *
	 * @access	public
	 * @return	void
	 */
	function workerDbjob(){
		$dbjob = $this->input->post('dbjob');
		$id = $this->input->post('id');
		$tel = $this->input->post('tel');
		$tel = (sizeof($tel) > 0) ? implode('-',$tel) : null;
		
		$hp = $this->input->post('hp');
		$hp = (sizeof($hp) > 0) ? implode('-',$hp) : null;
		
		$mail_id = $this->input->post('mail_id');
		$mail_domain = $this->input->post('mail_domain');
		$email = "{$mail_id}@{$mail_domain}";
		
		/*if(!get_magic_quotes_gpc()){
			$content = addslashes($content);
			$title = addslashes($title);
		}*/
	
		switch($dbjob){
			case('i'):
				$msg = '저장되었습니다.';
				$data = array(
						'duty' => $this->input->post('duty') ,
						'title' => $this->input->post('title') ,
						'name' => $this->input->post('name') ,
						'email' => $email,
						'tel'=> $tel,
						'hp'=> $hp
				);
			break;
			case('u'):
				$msg = '수정되었습니다.';
				$data = array(
						'duty' => $this->input->post('duty') ,
						'title' => $this->input->post('title') ,
						'name' => $this->input->post('name') ,
						'email' => $email,
						'tel'=> $tel,
						'hp'=> $hp
				);
			break;
			case('d'):
				$worker = $this->worker_model->select($id);
				if($worker['photo']){
					$old_file = explode('|',$worker['photo']);
					if(file_exists(convert_path('/upload/worker/'.$old_file[0]))){
						@unlink(convert_path('/upload/worker/'.$old_file[0]));
					}
				}
				
				$msg = '삭제 되었습니다.';
				$data = array('id' => $id);
			break;
		}
		
		if(isset($_FILES['files']) && $dbjob !='d'){
			/*file upload begin*/
			$upload_path = convert_path('/upload/worker');
			$config['upload_path'] = $upload_path;
			$config['allowed_types'] = '*';
			$config['max_size']	= '3072'; //3MB
			//$config['max_width']  = '1024';
			//$config['max_height']  = '768';
			$config['encrypt_name'] = true;
			$config['remove_spaces'] = true;
			$this->load->library('upload', $config);
			$this->upload->mkdir($upload_path);
			$file = $this->upload->do_multi_upload('files');
			
			$photo[] = $file[0]['file_name'];
			$photo[] = $file[0]['orig_name'];
				
			$photo_info = implode('|',$photo);
			$data = array_merge($data, array('photo'=> $photo_info));
		}
		
		$this->worker_model->dbjob($data);
		$this->alert->jalert($msg, base_url('/worker/workerList'));
	}
	
	
}

/* End of file html.php */
/* Location: ./application/controllers/member.php */